<?php
// Zerolith string Library - (c)2022 Courtesy Software
// 1.0 - 01/2023

class zstr
{
	//Adapted from wordpress; takes the accent off foreign input
	function westernize($string, $locale = null)
	{
        if(!preg_match('/[\x80-\xff]/', $string)) { return $string; } //optimization

        $chars=
        [
			'ª'=>'a','º'=>'o','À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Ä'=>'A','Å'=>'A','Æ'=>'AE','Ç'=>'C','È'=>'E','É'=>'E','Ê'=>'E','Ë'=>'E','Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','Ð'=>'D','Ñ'=>'N','Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ö'=>'O','Ù'=>'U','Ú'=>'U','Û'=>'U','Ü'=>'U','Ý'=>'Y','Þ'=>'TH','ß'=>'s','à'=>'a','á'=>'a','â'=>'a','ã'=>'a','ä'=>'a','å'=>'a','æ'=>'ae','ç'=>'c','è'=>'e','é'=>'e','ê'=>'e','ë'=>'e','ì'=>'i','í'=>'i','î'=>'i','ï'=>'i','ð'=>'d','ñ'=>'n','ò'=>'o','ó'=>'o','ô'=>'o','õ'=>'o','ö'=>'o','ø'=>'o','ù'=>'u','ú'=>'u','û'=>'u','ü'=>'u','ý'=>'y','þ'=>'th','ÿ'=>'y','Ø'=>'O','Ā'=>'A','ā'=>'a','Ă'=>'A','ă'=>'a','Ą'=>'A','ą'=>'a','Ć'=>'C','ć'=>'c','Ĉ'=>'C','ĉ'=>'c','Ċ'=>'C','ċ'=>'c','Č'=>'C','č'=>'c','Ď'=>'D','ď'=>'d','Đ'=>'D','đ'=>'d','Ē'=>'E','ē'=>'e','Ĕ'=>'E','ĕ'=>'e','Ė'=>'E','ė'=>'e','Ę'=>'E','ę'=>'e','Ě'=>'E','ě'=>'e','Ĝ'=>'G','ĝ'=>'g','Ğ'=>'G','ğ'=>'g','Ġ'=>'G','ġ'=>'g','Ģ'=>'G','ģ'=>'g','Ĥ'=>'H','ĥ'=>'h','Ħ'=>'H','ħ'=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'=>'u','Ỳ'=>'Y','ỳ'=>'y','Ả'=>'A','ả'=>'a','Ẩ'=>'A','ẩ'=>'a','Ẳ'=>'A','ẳ'=>'a','Ẻ'=>'E','ẻ'=>'e','Ể'=>'E','ể'=>'e','Ỉ'=>'I','ỉ'=>'i','Ỏ'=>'O','ỏ'=>'o','Ổ'=>'O','ổ'=>'o','Ở'=>'O','ở'=>'o','Ủ'=>'U','ủ'=>'u','Ử'=>'U','ử'=>'u','Ỷ'=>'Y','ỷ'=>'y','Ẫ'=>'A','ẫ'=>'a','Ẵ'=>'A','ẵ'=>'a','Ẽ'=>'E','ẽ'=>'e','Ễ'=>'E','ễ'=>'e','Ỗ'=>'O','ỗ'=>'o','Ỡ'=>'O','ỡ'=>'o','Ữ'=>'U','ữ'=>'u','Ỹ'=>'Y','ỹ'=>'y','Ấ'=>'A','ấ'=>'a','Ắ'=>'A','ắ'=>'a','Ế'=>'E','ế'=>'e','Ố'=>'O','ố'=>'o','Ớ'=>'O','ớ'=>'o','Ứ'=>'U','ứ'=>'u','Ạ'=>'A','ạ'=>'a','Ậ'=>'A','ậ'=>'a','Ặ'=>'A','ặ'=>'a','Ẹ'=>'E','ẹ'=>'e','Ệ'=>'E','ệ'=>'e','Ị'=>'I','ị'=>'i','Ọ'=>'O','ọ'=>'o','Ộ'=>'O','ộ'=>'o','Ợ'=>'O','ợ'=>'o','Ụ'=>'U','ụ'=>'u','Ự'=>'U','ự'=>'u','Ỵ'=>'Y','ỵ'=>'y','ɑ'=>'a','Ǖ'=>'U','ǖ'=>'u','Ǘ'=>'U','ǘ'=>'u','Ǎ'=>'A','ǎ'=>'a','Ǐ'=>'I','ǐ'=>'i','Ǒ'=>'O','ǒ'=>'o','Ǔ'=>'U','ǔ'=>'u','Ǚ'=>'U','ǚ'=>'u','Ǜ'=>'U','ǜ'=>'u',
        ];
		
	    // Used for locale-specific rules
	    if('de_DE' == $locale || 'de_DE_formal' == $locale || 'de_CH' == $locale || 'de_CH_informal' == $locale)
		{$chars['Ä']='Ae';$chars['ä']='ae';$chars['Ö']='Oe';$chars['ö']='oe';$chars['Ü']='Ue';$chars['ü']='ue';$chars['ß']='ss';}
		elseif('da_DK' === $locale)
		{$chars['Æ']='Ae';$chars['æ']='ae';$chars['Ø']='Oe';$chars['ø']='oe';$chars['Å']='Aa';$chars['å']='aa';}
		elseif('ca' === $locale) {$chars['l·l'] = 'll';}
		elseif('sr_RS' === $locale || 'bs_BA' === $locale) {$chars['Đ']='DJ';$chars['đ']='dj';}
		
	    return strtr($string, $chars);
	}
	
	//htmlentities but faster if you are unsure if a string contains HTML. Not fully safe yet.
	public static function sanitizeHTML($HTML, $shortHTML = false)
	{
		if(strpos($HTML, "<") === FALSE) { return $HTML; } //speed hack
		else { return htmlentities($HTML); }
	}
	
	//cut a string into ... after X characters
    public static function shorten($textString, $limit = 30)
    {
        if($limit < 4) { return $textString; }
        if(strlen($textString) > $limit) { $textString = substr($textString,0,($limit - 3)) . "..."; }
        return $textString;
    }
    
    //because php's function apparently doesn't work 100% of the time..
	public static function rn2br($textString)  { return str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br/>", $textString); }
	
	//we need a dramatically better method in the future, but it does work..
	public static function htmlToText($textString = "", $cropAtChr = 0)
	{
		$textString = nl2br($textString);
		$textString = str_replace("<br>", "+)(+", $textString); //use these characters to shield the result from strip_tags
		$textString = str_replace("</p>", "+)(++)(+", $textString);
		if($cropAtChr != 0){$textString = mb_strimwidth(strip_tags($textString), 0, $cropAtChr, "..."); }
		else { $textString = strip_tags($textString); }
		$textString = trim(str_replace("+)(+", "<br>", $textString), "<br>");
		$textString = str_replace("<br><br>", "<br>", $textString); //crappy hack
		$textString = trim($textString, "<br>"); //crappy hack
		$textString = trim($textString, "<br>"); //crappy hack
		return $textString;
	}
	
	//the opposite, crappier
	public static function textToHtml($textString){ return preg_replace('/\v+|\\\r\\\n/Ui','<br/>',$textString); }
	
	//strips everything to the right of a string, including the character itself. - needs testing
	function stripRightOf($inString, $clipAtString)
	{
		if(strpos($inString, $clipAtString) !== false ) { return substr($inString, 0, strpos($inString, $clipAtString)); }
		else { return $inString; }
	}
	
	//strips everything to the left of a string, including the character itself. - needs testing
	function stripLeftOf($inString, $clipAtString)
	{
		if(strpos($inString, $clipAtString) !== false ) { return substr($inString, -strpos($inString, $clipAtString)); }
		else { return $inString; }
	}
	
	//returns an array of text that is between the specified end and start tags. - needs testing
	function getBetweenTags($inString, $startTag, $endTag, $includeTags = false)
	{
	    $result = [];
		$chunks = explode($startTag, $inString);
	    foreach($chunks as $chunk)
		{
	        $pos = strpos($chunk, $endTag);
	        if($pos !== false)
			{
				if($includeTags) { $result[] = $startTag . substr($chunk, 0, $pos) . $endTag; }
				else { $result[] = substr($chunk, 0, $pos); }
			}
	    }
	    return $result;
	}
}
?>